package com.fashion.server.common;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

public class PagerTag extends TagSupport {
	
	//总页数
	private Integer pageTotal;
	//当前页数
	private Integer currentPage;
	
	//被提交表单的ID
	private String formId;
	
	private String sortColumn;
	private Integer sortOrder;

	private boolean skip=true;
	private boolean last=true;
	private boolean showPageTotal = true;

	public void setSkip(boolean skip) {
		this.skip = skip;
	}

	public void setLast(boolean last) {
		this.last = last;
	}


	public void setShowPageTotal(boolean showPageTotal) {
		this.showPageTotal = showPageTotal;
	}
	
	public void setSortColumn(String sortColumn) {
		this.sortColumn = sortColumn;
	}
	public void setSortOrder(Integer sortOrder) {
		this.sortOrder = sortOrder;
	}
	public void setPageTotal(Integer pageTotal) {
		this.pageTotal = pageTotal;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public void setFormId(String formId) {
		this.formId = formId;
	}
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			if(sortOrder==null){
				sortOrder=0;
			}
			out.print("<input type='hidden' name ='sortColumn' id='sortColumn' value='"+sortColumn+"'/>");
			out.print("<input type='hidden' name ='sortOrder' id='sortOrder' value='"+sortOrder+"'/>");

			out.print("<input type='hidden' name ='current' id='current' value='"+currentPage+"'/>");
			out.print("<div style='margin-top:10px'></div>");

			if (showPageTotal) {
				out.print("<div style='float:left' class='saga_page'>");
				out.print("&nbsp;共" + currentPage + "/" + pageTotal + "页</div>");
			}

			if(pageTotal!=null && pageTotal>1){
				out.print("<div style='float:right' id='pageDiv'></div>");
				out.print("<script type='text/javascript'>");
				out.print("laypage({");
				out.print("cont: 'pageDiv',");
				out.print("pages:"+pageTotal+",");
				out.print("curr:"+currentPage+",");
				if(skip){
					out.print("skip: true,");
				}else{
					out.print("skip: false,");
				}
				if(!last){
					out.print("last: false,");
				}
				out.print("jump: function(e, first){ ");
				out.print("if(!first){");
				out.print("$('#current').val(e.curr);");
				out.print("$('#"+formId+"').submit();");
				out.print("}}});");
				out.print("</script>");
			}else{
				out.print("<div style='float:right' id='pageDiv'>无更多数据</div>");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return super.doStartTag();
	}

}
